package com.vrv.app.cyberspace.modules.sys.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vrv.app.cyberspace.common.utils.HttpContextUtils;
import com.vrv.app.cyberspace.common.utils.JSONResult;
import com.vrv.app.cyberspace.modules.sys.entity.BaseTable;
import com.vrv.app.cyberspace.modules.sys.entity.InformationTable;
import com.vrv.app.cyberspace.modules.sys.entity.UserTable;
import com.vrv.app.cyberspace.modules.sys.mapper.BaseTableMapper;
import com.vrv.app.cyberspace.modules.sys.mapper.InformationTableMapper;
import com.vrv.app.cyberspace.modules.sys.mapper.InformationTypeTableMapper;
import com.vrv.app.cyberspace.modules.sys.service.IInformationTableService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.vrv.app.cyberspace.common.utils.JSONResult.renderError;
import static com.vrv.app.cyberspace.common.utils.JSONResult.renderSuccess;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sm
 * @since 2017-12-15
 */
@SuppressWarnings({"SpringJavaInjectionPointsAutowiringInspection", "Duplicates"})
@Service
@Transactional
public class InformationTableServiceImpl extends ServiceImpl<InformationTableMapper, InformationTable> implements IInformationTableService {
    Logger logger = LoggerFactory.getLogger(InformationTableServiceImpl.class);

    @Autowired
    private BaseTableMapper baseTableMapper;
    @Autowired
    private InformationTableMapper informationTableMapper;
    @Autowired
    InformationTypeTableMapper informationTypeTableMapper;

    @Override
    public JSONResult addInformation(BaseTable bt, InformationTable it) {
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        Date now = new Date();
        if (StringUtils.isEmpty(bt.getCreateUserID())){
            return renderError("createUserID参数不能为空！");
        }
        bt.setInfoType(1);
        bt.setCreateTime(now);
        bt.setUpdateTime(now);
        baseTableMapper.insert(bt);
        it.setBaseID(bt.getId());
        informationTableMapper.insert(it);
        logger.info("新增信息"+bt.getId()+"由"+userTable.getLinkdoodID()+"-"+userTable.getLinkdoodName()+"新增");
        return renderSuccess();
    }


    @Override
    public JSONResult updateInformation(Integer id, BaseTable bt, InformationTable it) {
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        bt.setId(id);
        bt.setUpdateTime(new Date());
        baseTableMapper.updateById(bt);
        it.setBaseID(id);
        informationTableMapper.updateById(it);
        logger.info("变更信息"+bt.getId()+"由"+userTable.getLinkdoodID()+"-"+userTable.getLinkdoodName()+"变更");
        return renderSuccess();
    }

}
